package base;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

@Slf4j(topic = "c.create thread")
public class CreateDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> futureTask = new FutureTask<>(() -> {
            log.debug("running");
            Thread.sleep(1000);
            return 1;
        });
        Thread t = new Thread(futureTask,"t3");
        t.start();
        //futureTask.get()是一个阻塞操作
        System.out.println(futureTask.get());
        log.debug("running");
    }

    //2.使用Runnable配合Thread
    private static void m2() {
        Runnable r = () -> log.debug("running");
        Thread t =new Thread(r,"t2");
        t.start();
        log.debug("running");
    }

    //1.直接使用Thread并重写run方法
    private static void m1() {
        Thread t = new Thread() {
            @Override
            public void run() {
                log.debug("running");
            }
        };
        t.setName("t1");
        t.start();

        log.debug("running");
    }
}
